Junio C Hamano | 2fbcd21 | 2008-05-14 22:26:07 | [diff] [blame] | 1 | gitcli(7) |
Junio C Hamano | f556fc2 | 2007-12-23 08:16:38 | [diff] [blame] | 2 | ========= |
| 3 | |
| 4 | NAME |
| 5 | ---- |
| 6 | gitcli - git command line interface and conventions |
| 7 | |
| 8 | SYNOPSIS |
| 9 | -------- |
| 10 | gitcli |
| 11 | |
| 12 | |
| 13 | DESCRIPTION |
| 14 | ----------- |
| 15 | |
Junio C Hamano | c4fec20 | 2008-06-28 10:07:24 | [diff] [blame] | 16 | This manual describes the convention used throughout git CLI. |
| 17 | |
| 18 | Many commands take revisions (most often "commits", but sometimes |
| 19 | "tree-ish", depending on the context and command) and paths as their |
| 20 | arguments. Here are the rules: |
| 21 | |
| 22 | * Revisions come first and then paths. |
| 23 | E.g. in `git diff v1.0 v2.0 arch/x86 include/asm-x86`, |
| 24 | `v1.0` and `v2.0` are revisions and `arch/x86` and `include/asm-x86` |
| 25 | are paths. |
| 26 | |
| 27 | * When an argument can be misunderstood as either a revision or a path, |
Junio C Hamano | b76a686 | 2012-05-02 22:02:46 | [diff] [blame] | 28 | they can be disambiguated by placing `--` between them. |
| 29 | E.g. `git diff -- HEAD` is, "I have a file called HEAD in my work |
Junio C Hamano | c4fec20 | 2008-06-28 10:07:24 | [diff] [blame] | 30 | tree. Please show changes between the version I staged in the index |
| 31 | and what I have in the work tree for that file". not "show difference |
| 32 | between the HEAD commit and the work tree as a whole". You can say |
Junio C Hamano | b76a686 | 2012-05-02 22:02:46 | [diff] [blame] | 33 | `git diff HEAD --` to ask for the latter. |
Junio C Hamano | c4fec20 | 2008-06-28 10:07:24 | [diff] [blame] | 34 | |
Junio C Hamano | b76a686 | 2012-05-02 22:02:46 | [diff] [blame] | 35 | * Without disambiguating `--`, git makes a reasonable guess, but errors |
Junio C Hamano | c4fec20 | 2008-06-28 10:07:24 | [diff] [blame] | 36 | out and asking you to disambiguate when ambiguous. E.g. if you have a |
| 37 | file called HEAD in your work tree, `git diff HEAD` is ambiguous, and |
Junio C Hamano | b76a686 | 2012-05-02 22:02:46 | [diff] [blame] | 38 | you have to say either `git diff HEAD --` or `git diff -- HEAD` to |
Junio C Hamano | c4fec20 | 2008-06-28 10:07:24 | [diff] [blame] | 39 | disambiguate. |
| 40 | |
| 41 | When writing a script that is expected to handle random user-input, it is |
| 42 | a good practice to make it explicit which arguments are which by placing |
Junio C Hamano | b76a686 | 2012-05-02 22:02:46 | [diff] [blame] | 43 | disambiguating `--` at appropriate places. |
Junio C Hamano | c4fec20 | 2008-06-28 10:07:24 | [diff] [blame] | 44 | |
| 45 | Here are the rules regarding the "flags" that you should follow when you are |
| 46 | scripting git: |
Junio C Hamano | f556fc2 | 2007-12-23 08:16:38 | [diff] [blame] | 47 | |
| 48 | * it's preferred to use the non dashed form of git commands, which means that |
Junio C Hamano | ea82cff | 2009-03-18 01:54:48 | [diff] [blame] | 49 | you should prefer `git foo` to `git-foo`. |
Junio C Hamano | f556fc2 | 2007-12-23 08:16:38 | [diff] [blame] | 50 | |
Junio C Hamano | ea82cff | 2009-03-18 01:54:48 | [diff] [blame] | 51 | * splitting short options to separate words (prefer `git foo -a -b` |
| 52 | to `git foo -ab`, the latter may not even work). |
Junio C Hamano | f556fc2 | 2007-12-23 08:16:38 | [diff] [blame] | 53 | |
| 54 | * when a command line option takes an argument, use the 'sticked' form. In |
Junio C Hamano | ea82cff | 2009-03-18 01:54:48 | [diff] [blame] | 55 | other words, write `git foo -oArg` instead of `git foo -o Arg` for short |
| 56 | options, and `git foo --long-opt=Arg` instead of `git foo --long-opt Arg` |
Junio C Hamano | f556fc2 | 2007-12-23 08:16:38 | [diff] [blame] | 57 | for long options. An option that takes optional option-argument must be |
| 58 | written in the 'sticked' form. |
| 59 | |
| 60 | * when you give a revision parameter to a command, make sure the parameter is |
| 61 | not ambiguous with a name of a file in the work tree. E.g. do not write |
Junio C Hamano | ea82cff | 2009-03-18 01:54:48 | [diff] [blame] | 62 | `git log -1 HEAD` but write `git log -1 HEAD --`; the former will not work |
Junio C Hamano | f556fc2 | 2007-12-23 08:16:38 | [diff] [blame] | 63 | if you happen to have a file called `HEAD` in the work tree. |
| 64 | |
Junio C Hamano | d031611 | 2012-08-22 19:55:29 | [diff] [blame] | 65 | * many commands allow a long option "--option" to be abbreviated |
| 66 | only to their unique prefix (e.g. if there is no other option |
| 67 | whose name begins with "opt", you may be able to spell "--opt" to |
| 68 | invoke the "--option" flag), but you should fully spell them out |
| 69 | when writing your scripts; later versions of Git may introduce a |
| 70 | new option whose name shares the same prefix, e.g. "--optimize", |
| 71 | to make a short prefix that used to be unique no longer unique. |
| 72 | |
Junio C Hamano | f556fc2 | 2007-12-23 08:16:38 | [diff] [blame] | 73 | |
Junio C Hamano | c4fec20 | 2008-06-28 10:07:24 | [diff] [blame] | 74 | ENHANCED OPTION PARSER |
| 75 | ---------------------- |
Junio C Hamano | f556fc2 | 2007-12-23 08:16:38 | [diff] [blame] | 76 | From the git 1.5.4 series and further, many git commands (not all of them at the |
| 77 | time of the writing though) come with an enhanced option parser. |
| 78 | |
| 79 | Here is an exhaustive list of the facilities provided by this option parser. |
| 80 | |
| 81 | |
| 82 | Magic Options |
| 83 | ~~~~~~~~~~~~~ |
| 84 | Commands which have the enhanced option parser activated all understand a |
| 85 | couple of magic command line options: |
| 86 | |
| 87 | -h:: |
| 88 | gives a pretty printed usage of the command. |
| 89 | + |
| 90 | --------------------------------------------- |
| 91 | $ git describe -h |
Junio C Hamano | e74124b | 2009-09-23 06:51:01 | [diff] [blame] | 92 | usage: git describe [options] <committish>* |
Junio C Hamano | f556fc2 | 2007-12-23 08:16:38 | [diff] [blame] | 93 | |
| 94 | --contains find the tag that comes after the commit |
| 95 | --debug debug search strategy on stderr |
| 96 | --all use any ref in .git/refs |
| 97 | --tags use any tag in .git/refs/tags |
| 98 | --abbrev [<n>] use <n> digits to display SHA-1s |
| 99 | --candidates <n> consider <n> most recent tags (default: 10) |
| 100 | --------------------------------------------- |
| 101 | |
| 102 | --help-all:: |
| 103 | Some git commands take options that are only used for plumbing or that |
| 104 | are deprecated, and such options are hidden from the default usage. This |
| 105 | option gives the full list of options. |
| 106 | |
| 107 | |
| 108 | Negating options |
| 109 | ~~~~~~~~~~~~~~~~ |
Junio C Hamano | ea82cff | 2009-03-18 01:54:48 | [diff] [blame] | 110 | Options with long option names can be negated by prefixing `--no-`. For |
| 111 | example, `git branch` has the option `--track` which is 'on' by default. You |
| 112 | can use `--no-track` to override that behaviour. The same goes for `--color` |
| 113 | and `--no-color`. |
Junio C Hamano | f556fc2 | 2007-12-23 08:16:38 | [diff] [blame] | 114 | |
| 115 | |
| 116 | Aggregating short options |
| 117 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 118 | Commands that support the enhanced option parser allow you to aggregate short |
Junio C Hamano | ea82cff | 2009-03-18 01:54:48 | [diff] [blame] | 119 | options. This means that you can for example use `git rm -rf` or |
| 120 | `git clean -fdx`. |
Junio C Hamano | f556fc2 | 2007-12-23 08:16:38 | [diff] [blame] | 121 | |
| 122 | |
| 123 | Separating argument from the option |
| 124 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 125 | You can write the mandatory option parameter to an option as a separate |
| 126 | word on the command line. That means that all the following uses work: |
| 127 | |
| 128 | ---------------------------- |
| 129 | $ git foo --long-opt=Arg |
| 130 | $ git foo --long-opt Arg |
| 131 | $ git foo -oArg |
| 132 | $ git foo -o Arg |
| 133 | ---------------------------- |
| 134 | |
Junio C Hamano | 4e27231 | 2008-01-08 09:13:21 | [diff] [blame] | 135 | However, this is *NOT* allowed for switches with an optional value, where the |
Junio C Hamano | f556fc2 | 2007-12-23 08:16:38 | [diff] [blame] | 136 | 'sticked' form must be used: |
| 137 | ---------------------------- |
| 138 | $ git describe --abbrev HEAD # correct |
| 139 | $ git describe --abbrev=10 HEAD # correct |
| 140 | $ git describe --abbrev 10 HEAD # NOT WHAT YOU MEANT |
| 141 | ---------------------------- |
| 142 | |
| 143 | |
Junio C Hamano | fce7c7e | 2008-07-02 03:06:38 | [diff] [blame] | 144 | NOTES ON FREQUENTLY CONFUSED OPTIONS |
| 145 | ------------------------------------ |
| 146 | |
| 147 | Many commands that can work on files in the working tree |
| 148 | and/or in the index can take `--cached` and/or `--index` |
| 149 | options. Sometimes people incorrectly think that, because |
| 150 | the index was originally called cache, these two are |
| 151 | synonyms. They are *not* -- these two options mean very |
| 152 | different things. |
| 153 | |
| 154 | * The `--cached` option is used to ask a command that |
| 155 | usually works on files in the working tree to *only* work |
| 156 | with the index. For example, `git grep`, when used |
| 157 | without a commit to specify from which commit to look for |
| 158 | strings in, usually works on files in the working tree, |
| 159 | but with the `--cached` option, it looks for strings in |
| 160 | the index. |
| 161 | |
| 162 | * The `--index` option is used to ask a command that |
| 163 | usually works on files in the working tree to *also* |
| 164 | affect the index. For example, `git stash apply` usually |
| 165 | merges changes recorded in a stash to the working tree, |
| 166 | but with the `--index` option, it also merges changes to |
| 167 | the index as well. |
| 168 | |
| 169 | `git apply` command can be used with `--cached` and |
| 170 | `--index` (but not at the same time). Usually the command |
| 171 | only affects the files in the working tree, but with |
| 172 | `--index`, it patches both the files and their index |
| 173 | entries, and with `--cached`, it modifies only the index |
| 174 | entries. |
| 175 | |
| 176 | See also http://marc.info/?l=git&m=116563135620359 and |
| 177 | http://marc.info/?l=git&m=119150393620273 for further |
| 178 | information. |
| 179 | |
Junio C Hamano | f556fc2 | 2007-12-23 08:16:38 | [diff] [blame] | 180 | GIT |
| 181 | --- |
Junio C Hamano | f7c042d | 2008-06-06 22:50:53 | [diff] [blame] | 182 | Part of the linkgit:git[1] suite |